﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Affiliates.FindArtInfo.Models;
using BusinessLayer.RegInfo;
using BusinessLayer.RegInfo.Entities;
using CommandLib;

namespace Affiliates.FindArtInfo.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }        

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";
            return View();
        }

        public ActionResult Login()
        {
            return View(new LoginModel());
        }

        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            LoginModel loginModel = new LoginModel();
            Partner partner = new PartnerBL().GetByPartnerCode(model.UserName);
            if (partner != null)
            {
                if (partner.Password == Encrypt.MD5(model.PassWord))
                {
                    if (partner.Status == Constants.StatusPartner.IS_ACTIVE)
                    {
                        Session[Constants.SessionKey.SESSION_PARTNER] = partner;
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        loginModel.MessageLogin = "Account is not active";
                    }
                }
                else
                {
                    loginModel.MessageLogin = "Invalid Login";
                }
            }
            else
            {
                loginModel.MessageLogin = "Invalid Login";
            }
            return View("Login", loginModel);
        }

        [HttpPost]
        public ActionResult ForgotPassword(LoginModel model)
        {
            LoginModel loginModel = new LoginModel();
            Partner partner = new PartnerBL().GetByPartnerCode(model.UserName);
            if (partner != null)
            {
                loginModel.MessageSendPass = "Login Details Sent To Email";
            }
            else
            {
                loginModel.MessageSendPass = "Username Not Found";
            }
            return View("Login", loginModel); 
        }
    }
}
